﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

using Please_starve.Common.Managers;

namespace Please_starve.Sprites
{
    public static class Sprite3DTypes
    {
        public static Sprite3D[] Sprites = new Sprite3D[64];

        public static Sprite3D Tree;
        public static Sprite3D Grass;

        static Sprite3DTypes()
        {
            Sprite3DTextureInformation treeInfo = new Sprite3DTextureInformation()
            {
                assetName = "TreeAnima",
                rows = 1,
                coloumns = 4,
            };
            treeInfo.framePositions = initializeFramePositions(treeInfo);
            Tree = new Sprite3D(0, SpriteAngle.Vertical, treeInfo);

            Sprite3DTextureInformation grassInfo = new Sprite3DTextureInformation()
            {
                assetName = "Grassland",
                rows = 1,
                coloumns = 1,
            };
            grassInfo.framePositions = initializeFramePositions(grassInfo);
            Grass = new Sprite3D(1, SpriteAngle.Horizontal, grassInfo);
        }

        /// <summary>
        /// Give the row/coloumn frames the procent(?) value between 0-1 where they're in the image.
        /// Could need support for x/y offsets and something to handle many animations in one sprite.
        /// </summary>
        /// <param name="spriteInfo"></param>
        /// <returns></returns>
        private static Vector2[][][] initializeFramePositions(Sprite3DTextureInformation spriteInfo)
        {
            Texture2D texture = Resources.GetTexture("Sprite3D/" + spriteInfo.assetName);

            int rows = spriteInfo.rows;
            int coloumns = spriteInfo.coloumns;
            float width = (float)texture.Width / (coloumns * (float)texture.Width);
            float height = (float)texture.Height / (rows * (float)texture.Height);


            Vector2[][][] framePositions = new Vector2[rows][][];
            for (int i = 0; i < rows; i++)
            {
                framePositions[i] = new Vector2[coloumns][];
                for (int j = 0; j < coloumns; j++)
                {
                    Console.WriteLine(new Vector2(width * j + width, height * i));
                    framePositions[i][j] = new Vector2[4]{
                        new Vector2(width * j, height * i), //topLeft
                        new Vector2(width * j + width, height * i),  //topRight
                        new Vector2(width * j, height * i + height),  //bottomLeft
                        new Vector2(width * j + width, height * i + height),   //bottomRight
                    };
                }
            }

            return framePositions;
        }
    }
}
